﻿using DICOM_PROJECT.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DICOM_PROJECT.Extension
{
    public class Pagination<T>
    {
        public static List<T> Page(List<T> data, PageModel page)
        {
            if(data.Count==0)
            {
                page.PageNumber = 0;
                page.PageCount = 0;
                return new List<T>();
            }
            int count = data.Count;
            page.PageCount = count / page.RowsPerPage;
            if (count % page.RowsPerPage > 0)
            {
                page.PageCount++;
            }
            if (page.PageNumber > page.PageCount)
            {
                page.PageNumber = page.PageCount;
            }
            else if(page.PageNumber < 1)
            {
                page.PageNumber = 1;
            }
            int skip = page.PageNumber > 1 ? page.PageNumber - 1 : 0;
            return data.Skip(skip * page.RowsPerPage).Take(page.RowsPerPage).ToList();
        }

        public static List<T> Page(IOrderedEnumerable<T> data, PageModel page)
        {
            if(data.Count()==0)
            {
                page.PageNumber = 0;
                page.PageCount = 0;
                return new List<T>();
            }
            int count = data.Count();
            page.PageCount = count / page.RowsPerPage;
            if (count % page.RowsPerPage > 0)
            {
                page.PageCount++;
            }
            if (page.PageNumber > page.PageCount)
            {
                page.PageNumber = page.PageCount;
            }
            else if (page.PageNumber < 1)
            {
                page.PageNumber = 1;
            }
            int skip = page.PageNumber > 1 ? page.PageNumber - 1 : 0;
            return data.Skip(skip * page.RowsPerPage).Take(page.RowsPerPage).ToList();
        }
    }
}